package com.tianyang.yukuaifuzs.business.auth.view;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.text.InputType;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.View;
import android.widget.EditText;
import android.widget.RelativeLayout;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.tianyang.yukuaifuzs.R;
import com.tianyang.yukuaifuzs.base.BaseTitleActivity;
import com.tianyang.yukuaifuzs.business.auth.adapter.BusinessInfoAdapter;
import com.tianyang.yukuaifuzs.business.auth.bean.BusinessOutput;
import com.tianyang.yukuaifuzs.business.auth.contract.AuthConfig;
import com.tianyang.yukuaifuzs.business.auth.contract.BusinessInfoContract;
import com.tianyang.yukuaifuzs.business.auth.presenter.BusinessInfoPresenter;
import com.tianyang.yukuaifuzs.common.interfaces.CommonConfig;
import com.tianyang.yukuaifuzs.common.view.SearchCustomView;
import com.tianyang.yukuaifuzs.module.widget.VerticalSwipeRefreshLayout;
import com.tianyang.yukuaifuzs.utils.SoftInputUtil;

import java.util.List;

import butterknife.BindView;

/**
 * Description：行业类别
 */
public class BusinessInfoActivity extends BaseTitleActivity implements BusinessInfoContract.View {

    @BindView(R.id.searchCv)
    SearchCustomView searchCv;
    @BindView(R.id.areaSearchCv)
    SearchCustomView areaSearchCv;
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    @BindView(R.id.swipeRefreshLayout)
    VerticalSwipeRefreshLayout vSwipeRefreshLayout;
    @BindView(R.id.emptyView)
    RelativeLayout emptyView;

    private EditText phoneNumberEt;
    private EditText areaStringEt;

    private BusinessInfoAdapter vBusinessInfoAdapter;

    private BusinessInfoContract.Presenter mPresenter;

    private int nowPage = CommonConfig.PAGE_NUM;

    @Override
    protected int getLayoutResID() {
        return R.layout.activity_business_info;
    }

    @Override
    protected void initView() {
        super.initView();
        searchCv.setInputType(InputType.TYPE_CLASS_TEXT, "请输入关键字搜索");
        phoneNumberEt = searchCv.findViewById(R.id.phoneNumberEt);
        areaSearchCv.setInputType(InputType.TYPE_CLASS_TEXT, "请输入城市名搜索");
        areaStringEt = areaSearchCv.findViewById(R.id.phoneNumberEt);

        String merchantType = getIntent().getStringExtra(AuthConfig.INTENT_MERCHANT_TYPE);
        String mccType = getIntent().getStringExtra(AuthConfig.INTENT_MCC_TYPE);
        String channel = getIntent().getStringExtra(AuthConfig.INTENT_CHANNEL);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        vBusinessInfoAdapter = new BusinessInfoAdapter(mccType);
        recyclerView.setAdapter(vBusinessInfoAdapter);
        vBusinessInfoAdapter.disableLoadMoreIfNotFullPage(recyclerView);
        vBusinessInfoAdapter.setPreLoadNumber(2);

        if (TextUtils.equals(AuthConfig.MCC_TYPE_BUSINESS, mccType)) {
            commonTitle.setTitle("选择行业类别");
        } else if (TextUtils.equals(AuthConfig.MCC_TYPE_ALI, mccType)) {
            commonTitle.setTitle("选择支付宝行业");
        } else if (TextUtils.equals(AuthConfig.MCC_TYPE_WX, mccType)) {
            commonTitle.setTitle("选择微信行业");
        } else if (TextUtils.equals(AuthConfig.MCC_TYPE_BANK, mccType)) {
            commonTitle.setTitle("选择开户支行");
            areaSearchCv.setVisibility(View.VISIBLE);
        }
        mPresenter = new BusinessInfoPresenter(this, merchantType, mccType, channel);
        mPresenter.getMcc(nowPage, phoneNumberEt.getText().toString(), areaStringEt.getText().toString());
    }

    @Override
    protected void setListener() {
        super.setListener();
        phoneNumberEt.setOnKeyListener(new View.OnKeyListener() {
            @Override
            public boolean onKey(View v, int keyCode, KeyEvent event) {
                if (keyCode == KeyEvent.KEYCODE_ENTER && event.getAction() == KeyEvent.ACTION_UP) {
                    SoftInputUtil.hideSoftInput(BusinessInfoActivity.this);
                    nowPage = CommonConfig.PAGE_NUM;
                    mPresenter.getMcc(nowPage, phoneNumberEt.getText().toString(), areaStringEt.getText().toString());
                    vBusinessInfoAdapter.setEnableLoadMore(true);
                }
                return false;
            }
        });
        areaStringEt.setOnKeyListener(new View.OnKeyListener() {
            @Override
            public boolean onKey(View v, int keyCode, KeyEvent event) {
                if (keyCode == KeyEvent.KEYCODE_ENTER && event.getAction() == KeyEvent.ACTION_UP) {
                    SoftInputUtil.hideSoftInput(BusinessInfoActivity.this);
                    nowPage = CommonConfig.PAGE_NUM;
                    mPresenter.getMcc(nowPage, phoneNumberEt.getText().toString(), areaStringEt.getText().toString());
                    vBusinessInfoAdapter.setEnableLoadMore(true);
                }
                return false;
            }
        });
        vSwipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                nowPage = CommonConfig.PAGE_NUM;
                mPresenter.getMcc(nowPage, phoneNumberEt.getText().toString(), areaStringEt.getText().toString());
                vBusinessInfoAdapter.setEnableLoadMore(true);
            }
        });
        vBusinessInfoAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                BusinessOutput output = ((BusinessInfoAdapter) adapter).getItem(position);
                if (output != null) {
                    Intent intent = new Intent();
                    intent.putExtra(AuthConfig.INTENT_BUSINESS_INFO, output);
                    setResult(Activity.RESULT_OK, intent);
                    finish();
                }
            }
        });
        vBusinessInfoAdapter.setOnLoadMoreListener(new BaseQuickAdapter.RequestLoadMoreListener() {
            @Override
            public void onLoadMoreRequested() {
                mPresenter.getMcc(nowPage, phoneNumberEt.getText().toString(), areaStringEt.getText().toString());
            }
        }, recyclerView);
    }

    @Override
    public void showMccView(List<BusinessOutput> outputs, boolean isFirst) {
        if (outputs != null && outputs.size() > 0) {
            if (isFirst) {
                vBusinessInfoAdapter.setNewData(outputs);
            } else {
                vBusinessInfoAdapter.addData(outputs);
            }
            nowPage++;
            recyclerView.setVisibility(View.VISIBLE);
            emptyView.setVisibility(View.GONE);
        } else if (isFirst) {
            recyclerView.setVisibility(View.GONE);
            emptyView.setVisibility(View.VISIBLE);
        }
    }

    @Override
    public void showLoadMore(String loadMore) {
        if (vSwipeRefreshLayout.isRefreshing()) {
            vSwipeRefreshLayout.setRefreshing(false);
        }
        switch (loadMore) {
            case CommonConfig.LOADMORECOMPLETE:
                vBusinessInfoAdapter.loadMoreComplete();
                break;
            case CommonConfig.LOADMOREEND:
                vBusinessInfoAdapter.loadMoreEnd();
                break;
            case CommonConfig.LOADMOREFAIL:
                vBusinessInfoAdapter.loadMoreFail();
                break;
        }
    }

    @Override
    protected boolean hasBindEventBus() {
        return false;
    }

    @Override
    public void setPresenter(BusinessInfoContract.Presenter presenter) {
        this.mPresenter = presenter;
    }

    @Override
    public Context getContext() {
        return this;
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mPresenter != null) {
            mPresenter.onDestroyPresenter();
            mPresenter = null;
        }
    }
}
